package com.smartwebx.fr.ft.service.ckeditor.test;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCoracle10G_INVOKEFUNCTION {
	Connection conn = null;
    Statement statement = null;
    ResultSet rs = null;
    CallableStatement stmt = null;

    String driver;
    String url;
    String user;
    String pwd;
    String sql;
    String startDate;
    String endDate;
    
    
    public JDBCoracle10G_INVOKEFUNCTION() throws SQLException
    {
        driver = "oracle.jdbc.driver.OracleDriver";
        url = "jdbc:oracle:thin:@36.33.24.68:1521:ORCL";
        // oracle 用户
        user = "wg";
        // oracle 密码
        pwd = "wg123";
        init();
        // mysid：必须为要连接机器的sid名称，否则会包以下错：
        // java.sql.SQLException: Io 异常: Connection
        // refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
        // 参考连接方式:
        // Class.forName( "oracle.jdbc.driver.OracleDriver" );
        // cn = DriverManager.getConnection(
        // "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
    }
    
    
    public void init() throws SQLException {
        System.out.println("oracle jdbc test");
        try {
            Class.forName(driver);
            System.out.println("driver is ok");
            conn = DriverManager.getConnection(url, user, pwd);
            System.out.println("conection is ok");
            statement = conn.createStatement();
            // conn.setAutoCommit(false);
            // 输入参数
            startDate = "2013-09-01 00:00:00";
            endDate="2013-09-30 00:00:00";
            // 调用函数
            stmt = conn.prepareCall("{? = call JSPHZHO(?,?)}");
            // stmt.registerOutParameter(1, java.sql.Types.FLOAT);
            // stmt.registerOutParameter(2, java.sql.Types.CHAR);
            stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
            stmt.setString(2, startDate);
            stmt.setString(3, endDate);
            stmt.executeUpdate();
            // 取的结果集的方式一：
            /*rs = ((OracleCallableStatement) stmt).getCursor(1);*/
            // 取的结果集的方式二：
            rs = (ResultSet) stmt.getObject(1);
      
            ResultSetMetaData resultMetaData = rs.getMetaData(); 
            int cols = resultMetaData.getColumnCount(); 
            String resultRowTitle = "";
            
            for (int i = 1; i <= cols; i++) { 
            	resultRowTitle += resultMetaData.getColumnName(i) + "   "; 
            } 
            System.out.println(resultRowTitle); 
            while (rs.next()) { 
            	String resultRow="";
            	for(int i=1;i<=cols;i++){
            		resultRow +=rs.getString(i)+"   ";
            	}
                System.out.println(resultRow);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        	stmt.close();
        	rs.close();
        	conn.close();
            System.out.println("close ");
        }
    }
    
    public static void main(String args[]) throws SQLException// 自己替换［］
    {
        new JDBCoracle10G_INVOKEFUNCTION();
    }


}
